package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.pojo.Bed;
import com.csun.cmny.provider.pojo.Building;
import com.csun.cmny.provider.pojo.Floor;
import com.csun.cmny.provider.pojo.Room;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.BedService;
import com.csun.cmny.provider.service.BuildingService;
import com.csun.cmny.provider.service.RoomService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 楼栋管理
 * */
@RestController
@RequestMapping("/Building")
public class BuildingController extends BaseController {
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private BedService bedService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /*
     * 这里的认证和当前登录的用户注解先预留，等完善了再进行修改
     * 1.判断该用户是否属于该机构人员
     * */
    //添加楼栋——只有楼栋信息
    @RequestMapping(value = "/addBuilding", method = RequestMethod.POST)
    public Wrapper addBuilding(Building building) {
        //校验
        if (StringUtils.isEmpty(building.getBuildingName())) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"楼栋名称不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.name.null", null), null);
        }

        if (building.getFloorNum() == null) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"楼栋楼层数量不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.floor.null", null), null);
        }
        if (building.getBedNoType() == null) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"床位编号方式不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Building.bedNoType.null", null), null);
        }
        //如果该楼栋带床位编号，就需要对编号字段进行校验
        if (building.getIsBuildNo() == null) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"是否带楼栋编号不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.isNo.null", null), null);
        }
        if (building.getIsBuildNo() == 1) {
            if (building.getBuildingNo() == null) {
//                return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"楼栋编号不能为空",null);
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.no.null", null), null);
            }
        }
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        //判断该楼栋名称是否已经在该社区中存在
        Building building1 = new Building();
        building1.setBuildingName(building.getBuildingName());
        building1.setHomeId(homeId);
        Building building2 = buildingService.selectByBuilding(building1);
        if (building2 != null) {
//            return WrapMapper.wrap(Wrapper.ERROR_CODE,"该名称的楼栋已经在该社区中存在", null);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("building.name.exsit", null), null);
        }
        // 校验楼栋编号是否重复
        Building building3 = new Building();
        building3.setBuildingNo(building.getBuildingNo());
        building3.setHomeId(homeId);
        int count = buildingService.selectCount(building3);
        if (count > 0) {
//            return WrapMapper.wrap(Wrapper.ERROR_CODE,"该编号的楼栋已经在该社区中存在", null);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("building.no.exsit", null), null);
        }

        //插入楼栋
        building.setHomeId(homeId);
        int n = buildingService.insert(building);
        if (n <= 0) {
//            return WrapMapper.wrap(Wrapper.ERROR_CODE,"添加失败", null);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("operation.failed", null), null);
        }
//        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,"添加成功", n);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
    }
    //添加楼栋——包含房间、床位
    @RequestMapping(value = "/addBdgRoomBed", method = RequestMethod.POST)
    public Wrapper addBdgRoomBed(@RequestBody Building building) {
        //校验
        if (StringUtils.isEmpty(building.getBuildingName())) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"楼栋名称不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.name.null", null), null);
        }
        if (building.getFloorNum() == null) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"楼栋楼层数量不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.floor.null", null), null);
        }
        if (building.getBedNoType() == null) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"床位编号方式不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Building.bedNoType.null", null), null);
        }
        //如果该楼栋带床位编号，就需要对编号字段进行校验
        if (building.getIsBuildNo() == null) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"是否带楼栋编号不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.isNo.null", null), null);
        }
        if (building.getIsBuildNo() == 1) {
            if (building.getBuildingNo() == null) {
//                return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"楼栋编号不能为空",null);
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.no.null", null), null);
            }
        }
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();

        Building building1 = new Building();
        building1.setBuildingName(building.getBuildingName());
        building1.setHomeId(homeId);
        Building building2 = buildingService.selectByBuilding(building1);
        if (building2 != null) {
//            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"该名称的楼栋已经在该社区中存在",null);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("building.name.exsit", null), null);
        }
        //获取修改者
        Long modifier =loginAuthDto.getUserId();
        building.setHomeId(homeId);
        building.setModifier(modifier);
        //插入楼栋
        int m = buildingService.insert(building);
        if (m <= 0) {
//            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"添加失败",null);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.add.fail", null), null);
        }
        //床位编码方式
        Integer bedNoType = building.getBedNoType();
        //是否带楼栋编号
        Integer isBuildNo = building.getIsBuildNo();
        //楼栋包含
        String buildNo = building.getBuildingNo();
        //楼栋楼层连接符
        String buildLink = building.getBuildLink();
        //楼层房间连接符
        String floorLink = building.getFloorLink();
        List<Floor> floorList = building.getFloorList();
        if (!CollectionUtils.isEmpty(floorList)) {
            for (int i = 0; i <floorList.size() ; i++) {
                int num = 0;
                Integer floorNo = floorList.get(i).getFloorNo();
                List<Room> roomList = floorList.get(i).getRoomList();
                for (int j = 0; j <roomList.size() ; j++) {
                    Room room = roomList.get(j);
                    room.setBuildingId(building.getBuildingId());
                    Integer bedNum = room.getBedNum();
                    //获取房间类别-几人间
                    Integer roomType = bedNum;
                    if (bedNum > 4) {
                        roomType = 4;
                    }
                    room.setRoomType(roomType);
                    String roomNo = "";
                    if (isBuildNo == 0) {//不带
                        if (j < 10) {
                            roomNo = buildNo + floorNo + "0" + (j + 1);
                        }else if (j >= 10){
                            roomNo = buildNo + floorNo + (j + 1);
                        }
                    }else if (isBuildNo == 1){//带

                        if (j < 10) {
                            roomNo = buildNo + buildLink + floorNo + floorLink + "0" + (j + 1);
                        }else if (j >= 10){
                            roomNo = buildNo + floorNo + (j + 1);
                        }
                    }
                    room.setHasToilet(0);
                    room.setRoomNo(roomNo);
                    room.setFloorNo(floorNo);
                    room.setModifier(modifier);
                    int n = roomService.insert(room);
//                    if (n <= 0) {
//                        System.out.println("*********************新增楼栋,添加房间时失败,room.getRoomNo：" + room.getRoomNo() + "*********************");
//                    }
                    for (int k = 0; k <bedNum ; k++) {
                        Bed bed = new Bed();
                        bed.setRoomId(room.getRoomId());
                        bed.setModifier(modifier);
                        if (bedNoType == 1){//按照房间计数
                            bed.setBedNo(k + 1);
                        } else if (bedNoType == 2) {//按照楼层计数
                            num = num + 1;
                            bed.setBedNo(num);
                        }
                        //插入床位
                        int b = bedService.insert(bed);
//                        if (b <= 0) {
//                            System.out.println("*********************新增楼栋,添加床位时失败,bed.getBedNo：" + bed.getBedNo() + "*********************");
//                        }
                    }
                }
            }
        }else {
            //插入楼栋
            building.setHomeId(homeId);
            building.setModifier(modifier);
            int n = buildingService.insert(building);
            if (n <= 0) {
//                return  WrapMapper.wrap(Wrapper.ERROR_CODE,"添加失败",null);
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.add.fail", null), null);
            }
//            return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"添加成功",n);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.add.success", null), null);
        }
//        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"添加成功",null);
        return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.add.success", null), null);
    }
    //根据id删除楼栋——只有楼栋信息
    @RequestMapping(value = "/delBuildingById", method = RequestMethod.POST)
    public Wrapper delBuildingById(Integer buildingId) {
        if (buildingId == null) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"楼栋id不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.id.null", null), null);
        }
        Room room = new Room();
        room.setBuildingId(buildingId);
        //判断该楼栋是否有房间，如果有房间就不能删除
        List<Room> roomList = roomService.selectRooms(room);

        if (!CollectionUtils.isEmpty(roomList)) {
//            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"该楼栋下有房间,无法删除",buildingId);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("building.room.exsit", null), null);
        }
        //删除楼栋
        int m = buildingService.deleteById(buildingId);
        if (m <= 0) {
//            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"没有此id",buildingId);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("building.not.exsit", null), null);
        }
//        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"删除成功",m);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.delete.sucess", null), m);
    }

    //修改楼栋——只能修改基本信息，无法再次初始化房间、床位信息；可以修改的：楼栋名称，楼栋编号
    @RequestMapping(value = "/updateBuilding", method = RequestMethod.POST)
    public Wrapper updateBuilding(Building building){
        //校验
        if (building.getBuildingId() == null) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"楼栋id不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.id.null", null), null);
        }
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        Building building2 = new Building();
        building2.setBuildingName(building.getBuildingName());
        building2.setHomeId(homeId);
        Building building3 = buildingService.selectByBuilding(building2);
        if (org.apache.commons.lang.StringUtils.isNotBlank(building.getBuildingName())) {
            //判断该楼栋名称是否已经在机构中存在
            if (building3 != null && !building3.getBuildingId().toString().equals(building.getBuildingId().toString())) {
//                return  WrapMapper.wrap(Wrapper.ERROR_CODE,"该楼栋名称已经在社区中存在",null);
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.name.exsit", null), null);
            }
        }
        if (building.getBedNoType() != null) {
            //判断是否对床位编号方式进行了修改
            if (building3 != null && building3.getBedNoType().intValue() != building.getBedNoType().intValue()) {
                //如果进行了修改需要判断该楼栋是否已经存在床位，如果已经存在则不能修改床位编号方式；如果没有床位则可以进行修改床位编号方式

            }
        }
        //修改
        int n = buildingService.updateById(building);
        if (n <= 0) {
//            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"修改失败",null);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.change.fail", null), null);
        }
//        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"修改成功",n);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.change.success", null), n);
    }

    //根据楼栋id查询楼栋信息——只有楼栋信息
    @RequestMapping(value = "/selectById", method = RequestMethod.POST)
    public Wrapper selectById(Integer buildingId) {
        //校验
        if (buildingId == null) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"楼栋id不能为空",buildingId);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.id.null", null), buildingId);
        }
        Building building = buildingService.selectById(buildingId);
        if (building == null) {
//            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"没有此id",buildingId);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("building.not.exsit", null), buildingId);
        }
//            return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"查询成功",building);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("operation.success", null), building);
    }
    //查询该机构的所有楼栋——只有楼栋信息
    @RequestMapping(value = "/showAll", method = RequestMethod.POST)
    public Wrapper showAll(Building building) {
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("homeId",homeId);
        queryMap.put("buildingName",building.getBuildingName());
        List<Building> buildingList = buildingService.showAll(queryMap);
//        if (CollectionUtils.isEmpty(buildingList)) {
//            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"该社区还未添加任何楼栋",null);
//        }
//        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"查询成功",buildingList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), buildingList);
    }
    //查询该机构的所有楼栋——包含房间、床位信息
    @RequestMapping(value = "/showAlls", method = RequestMethod.POST)
    public Wrapper showAlls(Building building) {
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        building.setHomeId(homeId);
        List<Building> buildingList = buildingService.showAlls(building);
        if (CollectionUtils.isEmpty(buildingList)) {
//            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"未查询到对应楼栋信息",null);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("building.not.exsit", null), null);
        }
        for (int i = 0; i <buildingList.size() ; i++) {
            Building building1 = buildingList.get(i);
            List<Room> roomList = building1.getRoomList();
            if (!CollectionUtils.isEmpty(roomList)) {
                //楼层集合
                List<Floor> floorList = new ArrayList<>();
                //按照层进行循环，获取每层对应的房间
                for (int j = 0; j <building1.getFloorNum() ; j++) {
                    Floor floor = new Floor();
                    List<Room> roomList1 = new ArrayList<>();
                    floor.setFloorNo(j + 1);
                    for (int k = 0; k <roomList.size() ; k++) {
                        if (roomList.get(k).getFloorNo() == (j + 1)) {
                            roomList1.add(roomList.get(k));
                        }
                    }
                    floor.setRoomList(roomList1);
                    floorList.add(floor);
                }
                building1.setFloorList(floorList);
            }
        }
//        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"查询成功",buildingList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), buildingList);
    }

    @RequestMapping(value = "/addBuildingLife", method = RequestMethod.POST)
    public Wrapper addBuildingLife(Building building) {
        //校验
        if (StringUtils.isEmpty(building.getBuildingName())) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"楼栋名称不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.name.null", null), null);
        }
        if (building.getFloorNum() == null) {
//            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"楼栋楼层数量不能为空",null);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("building.floor.null", null), null);
        }
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        //判断该楼栋名称是否已经在该社区中存在
        Building building1 = new Building();
        building1.setBuildingName(building.getBuildingName());
        building1.setHomeId(homeId);
        Building building2 = buildingService.selectByBuilding(building1);
        if (building2 != null) {
//            return WrapMapper.wrap(Wrapper.ERROR_CODE,"该名称的楼栋已经在该社区中存在", null);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("building.name.exsit", null), null);
        }

        //插入楼栋
        building.setHomeId(homeId);
        int n = buildingService.insert(building);
        if (n <= 0) {
//            return WrapMapper.wrap(Wrapper.ERROR_CODE,"添加失败", null);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("operation.failed", null), null);
        }
//        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,"添加成功", n);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), n);
    }

    @RequestMapping(value = "/buildingTree", method = RequestMethod.POST)
    public Wrapper buildingTree(@RequestBody Building building) {
        if(building.getHomeId() == null){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("homeId.not.exist", null), null);
        }
        List<Building> buildingList = buildingService.showAlls(building);
        if (CollectionUtils.isEmpty(buildingList)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("building.not.exsit", null), null);
        }
        for (int i = 0; i <buildingList.size() ; i++) {
            Building building1 = buildingList.get(i);
            List<Room> roomList = building1.getRoomList();
            if (!CollectionUtils.isEmpty(roomList)) {
                //楼层集合
                List<Floor> floorList = new ArrayList<>();
                //按照层进行循环，获取每层对应的房间
                for (int j = 0; j <building1.getFloorNum() ; j++) {
                    Floor floor = new Floor();
                    List<Room> roomList1 = new ArrayList<>();
                    floor.setFloorNo(j + 1);
                    for (int k = 0; k <roomList.size() ; k++) {
                        if (roomList.get(k).getFloorNo() == (j + 1)) {
                            roomList1.add(roomList.get(k));
                        }
                    }
                    floor.setRoomList(roomList1);
                    floorList.add(floor);
                }
                building1.setFloorList(floorList);
            }
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), buildingList);
    }

}
